// pages/order/order.js
var common = require("../../common/common.js").common;

var app = getApp();

Page({
    /**
   * 页面的初始数据
   */
    data: {
        nav: [ {
            id: 100,
            text: "全部"
        }, {
            id: 0,
            text: "申请中"
        }, {
            id: 1,
            text: "已同意"
        }, {
            id: 2,
            text: "已拒绝"
        } ],
        choose_nav: 100,
        a: 2,
        page_sign: "order",
        cart_count: 0,
        order: [],
        code: "",
        isIPX: "",
        page: 1
    },
    /**
   * 生命周期函数--监听页面加载
   */
    onLoad: function(options) {
        var isIPX = app.globalData.isIPX;
        this.setData({
            isIPX: isIPX,
            domain: app.globalData.domain.slice(0, -10)
        });
        let cart_count = 0;
        let cart = wx.getStorageSync("cart") || [];
        for (var i = 0; i < cart.length; i++) {
            cart_count += cart[i]["count"];
        }
        wx.setStorage({
            key: "cart_count",
            data: cart_count
        });
        this.setData({
            cart_count: cart_count
        });
        var choose_nav = options.choose_nav || 100;
        this.setData({
            choose_nav: choose_nav
        });
        this.getBase();
        this.getData();
    },
    /**
   * 生命周期函数--监听页面初次渲染完成
   */
    onReady: function() {},
    /**
   * 生命周期函数--监听页面显示
   */
    onShow: function() {
        let cart_count = 0;
        let cart = wx.getStorageSync("cart") || [];
        for (var i = 0; i < cart.length; i++) {
            cart_count += cart[i]["count"];
        }
        wx.setStorage({
            key: "cart_count",
            data: cart_count
        });
        this.setData({
            cart_count: cart_count
        });
    },
    /**
   * 生命周期函数--监听页面隐藏
   */
    onHide: function() {},
    /**
   * 生命周期函数--监听页面卸载
   */
    onUnload: function() {},
    /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
    onPullDownRefresh: function() {},
    /**
   * 页面上拉触底事件的处理函数
   */
    onReachBottom: function() {
        this.getData();
    },
    /**
   * 用户点击右上角分享
   */
    share: function(e) {
        var trade_no = e.currentTarget.dataset.trade;
        wx.navigateTo({
            url: "/pages/order_confirm/order_confirm?order_no=" + trade_no
        });
    },
    //全局基础配置信息
    getBase: function() {
        var that = this;
        common.request({
            endpoint: "set/app_base",
            method: "get",
            data: {},
            success: res => {
                wx.setNavigationBarColor({
                    frontColor: "#ffffff",
                    backgroundColor: res.extra.color_main
                });
                that.setData({
                    color_main: res.extra.color_main,
                    color_minor: res.extra.color_minor,
                    apptitle: res.extra.title,
                    style: res.extra.style
                });
            }
        });
    },
    choose_nav: function(e) {
        var id = e.currentTarget.dataset.id;
        var that = this;
        var openid = wx.getStorageSync("user").openid;
        this.setData({
            choose_nav: id,
            page: 1,
            order: []
        });
        that.getData();
    },
    getData: function() {
        var that = this;
        var openid = wx.getStorageSync("user").openid;
        var choose_nav = that.data.choose_nav;
        var page = that.data.page;
        common.request({
            endpoint: "My/myinfo/myafterorder_all",
            method: "post",
            data: {
                openid: openid,
                verify_status: choose_nav,
                page: page
            },
            success: res => {
                var data = that.data.order;
                let order = page == 1 ? res.extra : data.concat(res.extra);
                if (res.extra.length > 0) {
                    that.setData({
                        order: order
                    });
                    that.data.page = page + 1;
                }
            }
        });
    }
});